package dao;

import bean.PageRequest;
import bean.Public;
import bean.Result;
import bean.Student;
import utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 李夏
 * @version 1.0
 */
public class PublicInfoMapper {

    public Integer queryPublicAlready(Integer publicId) throws SQLException {
        Connection connection=null;
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;


            connection = DBUtil.getConnection();
            String sql = "select * from public where public_id=? ";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, publicId);
            resultSet = preparedStatement.executeQuery();

            if(resultSet.next())
                return resultSet.getInt("public_already");
            return 0;



    }

    public void updatePublic(Public publicInfo) {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Connection connection=null;
        try {
            connection=DBUtil.getConnection();
            String sql="update public set public_time=?,public_already=?,stu_id=?,stu_name=? where public_id=?";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1, publicInfo.getPublicTime());
            preparedStatement.setInt(2,publicInfo.getPublicAlready());
            preparedStatement.setInt(3,publicInfo.getStuId());
            preparedStatement.setString(4,publicInfo.getPublicName());
            preparedStatement.setInt(5,publicInfo.getPublicId());
            int result =preparedStatement.executeUpdate();
        } catch (SQLException e) {


        }
        DBUtil.close(connection,preparedStatement,resultSet);


    }

    public Public queryPublic(Integer publicId) throws SQLException {
        Connection connection = null;
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;


        connection = DBUtil.getConnection();
        String sql = "select * from public where public_id=? ";
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, publicId);
        resultSet = preparedStatement.executeQuery();

        Public public1 = null;
        if (resultSet.next()) {
            public1 = new Public();
            public1.setPublicId(resultSet.getInt("public_id"));
            public1.setPublicAlready(resultSet.getInt("public_already"));
            public1.setStuName(resultSet.getString("stu_name"));
            public1.setStuId(resultSet.getInt("stu_id"));
            public1.setPublicTime(resultSet.getInt("public_time"));

        }
        return public1;


    }
}
